/*
 * Copyright (C) 2014 MetaBrainz Foundation
 *
 * This file is part of MusicBrainz, the open internet music database,
 * and is licensed under the GPL version 2, or (at your option) any
 * later version: http://www.gnu.org/licenses/gpl-2.0.txt
 */

import $ from 'jquery';
import ko from 'knockout';
import test from 'tape';

import '../../lib/jquery.ui/ui/jquery-ui.custom.js';
import '../common/MB/Control/Autocomplete.js';

function autocompleteTest(name, callback) {
  test(name, function (t) {
    const $fixture = $('<div>').appendTo('body');
    const $input = $('<input>').attr('type', 'text').appendTo($fixture);

    ko.applyBindingsToNode($input[0], {autocomplete: {entity: 'artist'}});
    const $menu = $input.entitylookup('widget');

    callback(t, $input, $menu);

    $input.entitylookup('destroy');
    $fixture.add('.ui-widget').remove();
  });
}

$.ui.menu.prototype.delay = 0;

$.mb.entitylookup.prototype.options.delay = 0;

$.Widget.prototype._delay = function (handler) {
  return (typeof handler === 'string' ? this[handler] : handler).call(this);
};

$.mb.entitylookup.prototype.options.source = function (request, response) {
  const data = [
    {
      comment: 'Foo!',
      gid: 'ac467e15-90a4-424e-9d4b-dda9564a5b35',
      id: '123',
      name: 'Foo.',
      sortname: 'Foo?',
    },
    {
      current: 1,
      pages: 2,
    },
  ];

  this._lookupSuccess(response, data);
};

function blurAutocomplete($input) {
  if (document.activeElement === $input[0]) {
    $input.blur();
  }
}

function clickOnMenuItem($input, $menu, itemToClick) {
  const $item = $menu.find('.ui-menu-item' + itemToClick).find('a');

  $item.mouseenter();

  const mousedown = $.Event('mousedown');
  $item.trigger(mousedown);

  if (!mousedown.isDefaultPrevented()) {
    blurAutocomplete($input);
  }

  $item.mouseup().click();
}

function searchAndClick(t, $input, $menu, itemToClick) {
  $input.val('Foo').keydown().focus();

  t.ok($menu.is(':visible'), 'menu is open after search');

  clickOnMenuItem($input, $menu, itemToClick);
}

autocompleteTest((
  'clicking on actions should not close the menu (MBS-6912)'
), function (t, $input, $menu) {
  t.plan(2);

  searchAndClick(t, $input, $menu, ':contains(Show more...)');

  t.ok($menu.is(':visible'), 'menu is still open after clicking show more');
});

autocompleteTest((
  'multiple searches should not prevent clicks on the menu (MBS-7080)'
), function (t, $input, $menu) {
  t.plan(3);

  searchAndClick(t, $input, $menu, ':eq(0)');
  searchAndClick(t, $input, $menu, ':eq(0)');

  t.ok($menu.is(':hidden'), 'menu is hidden after selecting item');
});
